﻿using LDT.Task.JudgetDoc.Generation.Model.System;
using LdtJudge.DataEngine.Common.CommHelp;
using LdtJudge.DataEngine.Data;
using LdtJudge.DataEngine.Model.DataModel.CommonManageModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.RegCommon.ConvertModeAnalysis
{
    ///<summary>
    /// 类名称  : ConvertATCode
    /// 功能    : 转换为字典方式
    ///</summary> 
    public class ConvertATCode : IConvertAnalysis
    {
        /// <summary>
        /// 案由ID
        /// </summary>
        public string ccid { get; set; }
        SqlSysCommon sqlSyscom = new SqlSysCommon("Ldt_JudgeDocSystem");

        /// <summary>
        /// 进行字典值的转换
        /// </summary>
        /// <param name="mode">转换数据配置对象</param>
        /// <param name="valuetext">需要转换的值内容</param>
        /// <param name="casetype">案件类型</param> 
        /// <returns></returns>
        public IList<string> ConvertValue(ConvertMode mode, string valuetext, string casetype,string casecuaseID)
        {
            if (mode != null)
            {
                ArgumentRel argument = new ArgumentRel();
                List<ArgumentRel> argumentRelList = sqlSyscom.GetArgumentRel(mode.CM_ID);
                if (argumentRelList.FirstOrDefault(a=> a.AR_CaseType==casetype)!=null)
                {
                    if (string.IsNullOrEmpty(casecuaseID))
                    {
                        argument = argumentRelList.FirstOrDefault(a => a.AR_CaseType == casetype);
                    }
                    else
                    {
                        ArgumentRel argumentRel = argumentRelList.FirstOrDefault(a => a.AR_CaseType == casetype && a.AR_CauseCode==casecuaseID);
                        if (argumentRel!=null)
                        {
                            argument = argumentRel;
                        }
                        else
                        {
                            argument = argumentRelList[0];
                        }
                    }
                    
                }
                else
                {
                    argument = argumentRelList[0];
                }
                //获取字典的转换方式
                string consMode = sqlSyscom.GetArgumentType_ConstituteMode(argument.AT_ID);

                if (!string.IsNullOrEmpty(ccid) && mode.CM_CodeCH== "量刑增加类型")
                {
                    string id= sqlSyscom.GetArgumentATidValue(argument.AT_Code, ccid);
                    if (!string.IsNullOrEmpty(id))
                    {
                        argument.AT_ID = id;
                    }
                }
                //获取当前字典编号类型关联的字典值集合;
                //List<LdtJudge.DataEngine.Model.DataModel.SystemtableModel.SYS_Argument> arguments = sqlSyscom.GetArgumentTableValue(mode.AT_ID, casetype);
                //List<LdtJudge.DataEngine.Model.DataModel.SystemtableModel.SYS_Argument> argumentsModel = sqlSyscom.GetArgumentTableValue(mode.AT_ID, casetype);
                List<LdtJudge.DataEngine.Model.DataModel.SystemtableModel.SYS_Argument> argumentsModel = sqlSyscom.GetArgumentTableValue(argument.AT_ID);
                //List<LdtJudge.DataEngine.Model.DataModel.SystemtableModel.SYS_Argument> argumentsModel = new List<LdtJudge.DataEngine.Model.DataModel.SystemtableModel.SYS_Argument>();
                //if (arguments != null)
                //{
                //    foreach (LdtJudge.DataEngine.Model.DataModel.SystemtableModel.SYS_Argument row in arguments)
                //    {
                //        var model = sqlSyscom.CopySameFieldsObject<LdtJudge.DataEngine.Model.DataModel.SystemtableModel.SYS_Argument>(row);
                //        if (model != null)
                //        {
                //            argumentsModel.Add(model);
                //        }
                //    }
                //}
                switch (consMode)
                {
                    case "1":
                        //标准字典词构成
                        return new List<string>() { argumentsModel.FirstOrDefault(mo => mo.AM_Name == valuetext)?.AM_Code };
                    case "2":
                        //同义词字典构成
                        string matchword = "";
                        foreach (var argum in argumentsModel)
                        {
                            matchword += argum.AM_Code + "=" + argum.AM_SameWords + ";";
                        }
                        return new List<string>() { DictionariesConver.GetArgumentValueByMatchWords(matchword, valuetext) };
                    default:
                        //字典构成模式错误！
                        throw new NotImplementedException();
                }
            }
            throw new NotImplementedException();

        }
    }
}
